package com.hdy.oa.action;

import com.hdy.DBUtils.JDBC;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/dept/login","/dept/logout","/dept/relog"})
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();

        if ("/dept/login".equals(servletPath)) {
            doLogin(request,response);
        } else if ("/dept/logout".equals(servletPath)) {
            doLogout(request,response);
        } else if ("/dept/relog".equals(servletPath)) {
            doRelog(request,response);
        }


    }

    private void doRelog(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String contextPath = request.getContextPath();
        Cookie[] cookies = request.getCookies();
        String username=null;
        String password=null;

        if (cookies != null) {
            for(Cookie cookie:cookies){
                String name = cookie.getName();
                if("username".equals(name)){
                    username=cookie.getValue();
                } else if ("password".equals(name)) {
                    password=cookie.getValue();
                }
            }
        }
        if(password!=null&&username!=null){
            HttpSession session = request.getSession();
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            response.sendRedirect(contextPath+"/dept/list");
        }else{
            response.sendRedirect(contextPath+"/login.jsp");
        }
    }

    private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
            //获取session对象，销毁session
                    HttpSession session = request. getSession(false) ;
                    if (session != null) {
            //手动销毁session对象。
                    session. invalidate() ;

            //销毁cookie (退出系统将所有的cookie全部销毁)
            方案1：
                    Cookie[] cookies = request. getCookies() ;
                    if (cookies != null) {
                    for (Cookie cookie : cookies) {
             //设置cookie的有效期为0，表示删除该cookie
                    cookie. setMaxAge(0);
             //设置一个下cookie的路径
                    cookie . setPath(request. getContextPath()); //删除cookie的时候注 意路径问题。
            //响应cookie给浏览器， 浏览器端会将之前的cookie覆盖。
                    response . addCookie (cookie);
                    }
               }
               方案2：
               //覆盖原来的cookie
               Cookie cookie1 = new Cookie( "username","");
                cookie1. setMaxAge(0);
                cookie1. setPath(request . getContextPath());
                Cookie cookie2 = new Cookie("password","");
                cookie2. setMaxAge(0);
                cookie2. setPath(request . getContextPath());
                response . addCookie (cookie1);
                response . addCookie (cookie2);

        */

        HttpSession session = request.getSession();
        session.removeAttribute("username");
        session.removeAttribute("password");
        response.sendRedirect(request.getContextPath());
    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {


        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String flag = request.getParameter("flag");

        String contextPath = request.getContextPath();
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        boolean success=false;

        try {
            connection = JDBC.getConnection();
            String sql = "select * from t_user where username=? and password=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if (rs.next() ) {
                //登陆成功
                HttpSession session = request.getSession();
                session.setAttribute("username",username);
                session.setAttribute("password",password);
                success=true;
            }else{
                //登陆失败
                response.sendRedirect(contextPath+"/login.jsp");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps,rs);
        }
        if(success){
            if(flag!=null){
                Cookie cookie1=new Cookie("username",username);
                Cookie cookie2=new  Cookie("password",password);
                cookie1.setPath(contextPath);
                cookie2.setPath(contextPath);
                cookie1.setMaxAge(60*60*24*10);
                cookie2.setMaxAge(60*60*24*10);
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            response.sendRedirect(contextPath+"/dept/list");
        }
    }
}
